package com.shph.validation.rule.impl;

import com.shph.validation.exception.EmailFormatterException;
import com.shph.validation.exception.NotMatchRegexException;
import com.shph.validation.rule.IRule;

import java.util.regex.Pattern;

/**
 * 邮箱校验
 */
public class EmailRule implements IRule<String> {
    @Override
    public boolean test(String value, String refValue) {
        String regex = "(?i)^[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\\.][a-z]{2,3}([\\.][a-z]{2})?$";
        boolean matches = Pattern.matches(regex, value);
        if (!matches) {
            throw new EmailFormatterException();
        }
        return true;
    }
}
